var urlorder = 'http://localhost:8080/service/Order.groovy';
getUserInfo = function() {
	var cookie;
	var resp;
	cookie = new cookiejar('tienda_virtual');
	currentUsr = cookie.getCookie('username');
	currentPass = cookie.getCookie('password');
	currentToken = cookie.getCookie('token');
	
	
	var req = new Ajax.Request(urlsecurity, {
		method: 'post',
		parameters: {method: 'GetAccount', username: currentUsr, authentication_token: currentToken},
		onSuccess: function(transport){
			resp = transport.responseXML;
						
			// username
			var respValue = resp.getElementsByTagName('username')[0].firstChild;
			var element = document.getElementsByName('username');			
			element[0].setAttribute('value', respValue.nodeValue);
			
			// name
			respValue = resp.getElementsByTagName('name')[0].firstChild;
			currentName = respValue.nodeValue;
			element = document.getElementsByName('name');
			element[0].setAttribute('value', respValue.nodeValue);
			
			// passwordDiv
			element = document.getElementsByName('oldPassword');
			element[0].setAttribute('value', currentPass);
			
			// email
			respValue = resp.getElementsByTagName('email')[0].firstChild;
			element = document.getElementsByName('email');
			currentEmail = respValue.nodeValue;
			element[0].setAttribute('value', respValue.nodeValue);
			
			// birth date
			respValue = resp.getElementsByTagName('birth_date')[0].firstChild;
			element = document.getElementsByName('birth_date');
			currentBirthDate = respValue.nodeValue;
			element[0].setAttribute('value', respValue.nodeValue);
			
		},
		onFailure: function(){ displayAlert('Sorry, we have a little problem with the server, please try later')}
  });
};

resetInfoForm = function() {
	inputUsername.attr("value", "");
	inputName.attr("value", "");
	inputOldPass.attr("value", "");
	inputNewPass.attr("value", "");
	inputConfirmPass.attr("value", "");
	inputEmail.attr("value", "");
	inputBirthDate.attr("value", "");
};

submitNewAccount = function() {
	
	var xml;
	if(validateUsername() && validateName() 
			&& validateOldPass() && validateConfirmPass() 
			&& validateEmail() && validateBirthDate()) {
		
		currentName = inputName.val();
		currentUsr = inputUsername.val();
		currentPass = inputNewPass.val();
		currentEmail = inputEmail.val();
		currentBirthDate = inputBirthDate.val();
		
		xml = '<account><username>'+currentUsr+'</username><name>'+currentName+'</name><password>'+currentPass+'</password><email>'+currentEmail+'</email><birth_date>'+currentBirthDate+'</birth_date></account>';
		
		var req = new Ajax.Request(urlsecurity, {
			method: 'post',
			parameters: {method: 'CreateAccount', account: xml},
			onSuccess: function(transport){
				resp = transport.responseXML;
				
				if(checkResp(resp)){
					displayTextPopUp("Welcome " + currentUsr, "Welcome to virtual shop, enjoy shopping!");
					signin(currentUsr, currentPass);
				}
			},
			
			onFailure: function(){ displayAlert('Sorry, we have a little problem with the server, please try later')}
			
		});
	}
	else
		displayAlert('Sorry, please check your information');
};


submitInfoForm = function() {
	var xml;
	if(currentName != inputName.val() 
			|| currentEmail != inputEmail.val() 
			|| currentBirthDate != inputBirthDate.val()) {
		// hay que hacer un updateAccount
		if(validateName() && validateEmail && validateBirthDate) {
			currentName = inputName.val();
			currentEmail = inputEmail.val();
			currentBirthDate = inputBirthDate.val();
			
			xml = '<account><name>'+currentName+'</name><email>'+currentEmail+'</email>'+'<birth_date>'+currentBirthDate+'</birth_date></account>';
			
			
			var req = new Ajax.Request(urlsecurity, {
				method: 'post',
				parameters: {method: 'UpdateAccount', username: currentUsr, authentication_token: currentToken, account: xml},
				onSuccess: function(transport){
					resp = transport.responseXML;
								
					// username
					var respValue = resp.firstChild.getAttribute("status");
					if(!checkResp(resp)){
						return;
						
					}
					
					
					if(respValue != 'ok') {
						displayAlert('Sorry, we have a little problem with the server, please try later');
					}
					else
						document.location.href = 'myAccount.html';
					
				},
				
				onFailure: function(){ displayAlert('Sorry, we have a little problem with the server, please try later')}
				
			});
		}
	}
	
	if(currentPass != inputOldPass.val()) {
		displayAlert('please complete your old password');
	}
	else {
		if(inputNewPass.val() != '') {
			var req = new Ajax.Request(urlsecurity, {
				method: 'post',
				parameters: {method: 'ChangePassword', username: currentUsr, password: currentPass, new_password: inputNewPass.val()},
				onSuccess: function(transport){
					resp = transport.responseXML;
								
					// username
					var respValue = resp.firstChild.getAttribute("status");
					
					displayAlert(respValue);
					if(respValue != 'ok') {
						alert('Sorry, we have a little problem with the server, please try later');
					}
					
				},
				
				onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
				
			});
		}
	}
	
};

getAddressList = function() {
	var cookie;
	var resp;
	
	var countryXml;
	var stateXml;
	
	cookie = new cookiejar('tienda_virtual');
	cookie.read();
	currentUsr = cookie.getCookie('username');
	currentPass = cookie.getCookie('password');
	currentToken = cookie.getCookie('token');
	
	var addressListDiv = document.getElementById('addressListDiv');
	
	var req = new Ajax.Request(urlorder, {
		method: 'post',
		parameters: {method: 'GetAddressList', username: currentUsr, authentication_token: currentToken},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(checkResp(resp)){
				
				var span = new Array('<span>full name:</span>', '<span>address: </span>', '<span>extra information</span>', '<span>country</span>', '<span>state: </span>', '<span>city: </span>', '<span>zip code: </span>', '<span>phone number: </span>');
				var addList = resp.getElementsByTagName('address');
				var tr;
				var td1;
				var td2;
				
				for(var i=0; i<addList.length; i++) {
					var eachAddrDiv = document.createElement('div');
					eachAddrDiv.innerHTML = '<table><tbody id="eachAddrTbody'+i+'"></tbody></table>';
					addressListDiv.appendChild(eachAddrDiv);
					var eachAddrTbody = document.getElementById('eachAddrTbody'+i);
					
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td1.innerHTML = span[0];					
					td2.innerHTML = addList[i].getElementsByTagName('full_name')[0].firstChild.nodeValue;
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td1.innerHTML = span[1];
					td2.innerHTML = addList[i].getElementsByTagName('address_line_1')[0].firstChild.nodeValue;
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td1.innerHTML = span[2];
					if(addList[i].getElementsByTagName('address_line_2')[0].firstChild == null)
						td2.innerHTML = '';
					else
						td2.innerHTML = addList[i].getElementsByTagName('address_line_2')[0].firstChild.nodeValue;
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'country'+i);
					td1.innerHTML = span[3];
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					var c_id = addList[i].getElementsByTagName('country_id')[0].firstChild.nodeValue;
					var s_id = addList[i].getElementsByTagName('state_id')[0].firstChild.nodeValue;
					completeCountry(c_id, s_id, i);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'state'+i);
					td1.innerHTML = span[4];
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'state'+i);
					td1.innerHTML = span[5];					
					td2.innerHTML = addList[i].getElementsByTagName('city')[0].firstChild.nodeValue;
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'state'+i);
					td1.innerHTML = span[6];
					td2.innerHTML = addList[i].getElementsByTagName('zip_code')[0].firstChild.nodeValue;
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'state'+i);
					td1.innerHTML = span[7];
					td2.innerHTML = addList[i].getElementsByTagName('phone_number')[0].firstChild.nodeValue;
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
				}
			}
			
		},
		
		onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
		
	});
};

getEditAddressList = function() {
	var cookie;
	var resp;
	
	var countryXml;
	var stateXml;
	
	cookie = new cookiejar('tienda_virtual');
	cookie.read();
	currentUsr = cookie.getCookie('username');
	currentPass = cookie.getCookie('password');
	currentToken = cookie.getCookie('token');
	
	var addressListDiv = document.getElementById('addressListDiv');
	
	var req = new Ajax.Request(urlorder, {
		method: 'post',
		parameters: {method: 'GetAddressList', username: currentUsr, authentication_token: currentToken},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(checkResp(resp)){
				
				var span = new Array('<span>full name:</span>', '<span>address: </span>', '<span>extra information: </span>', '<span>country: </span>', '<span>state: </span>', '<span>city: </span>', '<span>zip code: </span>', '<span>phone number: </span>');
				var addList = resp.getElementsByTagName('address');
				var tr;
				var td1;
				var td2;
				
				for(var i=0; i<addList.length; i++) {
					var eachAddrDiv = document.createElement('div');
					eachAddrDiv.innerHTML = '<table><tbody id="eachAddrTbody'+i+'"></tbody></table>';
					addressListDiv.appendChild(eachAddrDiv);
					var eachAddrTbody = document.getElementById('eachAddrTbody'+i);
					
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					input = document.createElement('input');
					input.setAttribute('id', 'inputFull_name'+i);
					input.setAttribute('value', addList[i].getElementsByTagName('full_name')[0].firstChild.nodeValue);
					td1.innerHTML = span[0];					
					td2.appendChild(input);
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					input = document.createElement('input');
					input.setAttribute('id', 'inputAddress_line_1'+i);
					input.setAttribute('value', addList[i].getElementsByTagName('address_line_1')[0].firstChild.nodeValue);
					td1.innerHTML = span[1];
					td2.appendChild(input);
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					input = document.createElement('input');
					input.setAttribute('id', 'inputAddress_line_2'+i);
					if(addList[i].getElementsByTagName('address_line_2')[0].firstChild == null)
						input.setAttribute('value', '');
					else
						input.setAttribute('value', addList[i].getElementsByTagName('address_line_2')[0].firstChild.nodeValue);
					input.setAttribute('value', addList[i].getElementsByTagName('address_line_1')[0].firstChild.nodeValue);
					td1.innerHTML = span[2];
					td2.appendChild(input)
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'countryList'+i);
					td1.innerHTML = span[3];
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					var c_id = addList[i].getElementsByTagName('country_id')[0].firstChild.nodeValue;
					var s_id = addList[i].getElementsByTagName('state_id')[0].firstChild.nodeValue;
					addCountryList2(i, c_id, s_id);
		
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					td2.setAttribute('id', 'selectState'+i);
					td1.innerHTML = span[4];
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					input = document.createElement('input');
					input.setAttribute('id', 'inputCity'+i);
					input.setAttribute('value', addList[i].getElementsByTagName('city')[0].firstChild.nodeValue);
					td2.appendChild(input);
					td1.innerHTML = span[5];					
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					input = document.createElement('input');
					input.setAttribute('id', 'inputZip_code'+i);
					input.setAttribute('value', addList[i].getElementsByTagName('zip_code')[0].firstChild.nodeValue);
					td1.innerHTML = span[6];
					td2.appendChild(input);
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					td2 = document.createElement('td');
					input = document.createElement('input');
					input.setAttribute('id', 'inputPhone_number'+i);
					input.setAttribute('value', addList[i].getElementsByTagName('phone_number')[0].firstChild.nodeValue);
					td1.innerHTML = span[7];
					td2.appendChild(input);
					tr.appendChild(td1);
					tr.appendChild(td2);
					eachAddrTbody.appendChild(tr);
					
					var saveDiv = document.createElement('div');
					var saveButton = document.createElement('input');
					saveButton.setAttribute('type', 'button');
					saveButton.setAttribute('value', 'save');
					saveButton.setAttribute('onclick', 'saveAddress('+i+');');
					saveDiv.appendChild(saveButton);
					eachAddrDiv.appendChild(saveDiv);
					
					tr = document.createElement('tr');
					td1 = document.createElement('td');
					var add_id = addList[i].getAttribute('id');
					td1.innerHTML = '<span style="visibility: hidden" id="address_id'+i+'">'+add_id+'</span>';
					tr.appendChild(td1);
					eachAddrTbody.appendChild(tr);
					
				}
			}
			
		},
		
		onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
		
	});
};

addCountryList2 = function(j, c_id, s_id) {
	var resp;
	var select = document.createElement("select");
	select.setAttribute("name", "country");
	select.setAttribute("id", "inputCountry"+j);
	select.setAttribute("onchange", 'addStateList2('+j+')');
	var option;

	var req = new Ajax.Request(urlcommon, {
		method: 'post',
		parameters: {method: 'GetCountryList', language_id: '1'},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(!checkResp(resp)){
				return;
			}
			
			var countryList2 = resp.getElementsByTagName('country');
			var tr;
			
			option = document.createElement('option');
			option.innerHTML = 'select a country';
			option.setAttribute('value', '');
			select.appendChild(option);
			tr = document.getElementById("countryList"+j);
			tr.appendChild(select);
			
			for(var i=0; i<countryList2.length; i++) {
				option = document.createElement('option');
				var aux = countryList2[i].lastElementChild.childNodes[0].nodeValue;
				option.innerHTML = aux;
				option.setAttribute('value', countryList2[i].getAttribute('id'));
				select.appendChild(option);
				tr = document.getElementById("countryList"+j);
				tr.appendChild(select);
			}

			
		},
		onFailure: function(){ Alert('Sorry, we have a little problem with the server, please try later');}
  });
};

addStateList2 = function(j) {

	var resp;
	var select = document.createElement("select");
	select.setAttribute("name", "state");
	select.setAttribute("id", "inputState"+j);
	var option;

	var selectedCountryId = document.getElementById("countryList"+j).firstChild.value;
	
	var req = new Ajax.Request(urlcommon, {
		method: 'post',
		parameters: {method: 'GetStateList', language_id: '1', country_id: selectedCountryId},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(!checkResp(resp)){
				return;
			}
			
			var stateList = resp.getElementsByTagName('state');
			var tr;
			
			option = document.createElement('option');
			option.innerHTML = 'select state';
			option.setAttribute('value', '');
			select.appendChild(option);
			tr = document.getElementById("selectState"+j);
			
			if(tr.firstChild != null){
				tr.removeChild(tr.firstChild);
			}
			
			tr.appendChild(select);
			
			
			for(var i=0; i<stateList.length; i++) {
				option = document.createElement('option');
				var aux = stateList[i].lastElementChild.childNodes[0].nodeValue;
				option.innerHTML = aux;
				option.setAttribute('value', stateList[i].getAttribute('id'));
				select.appendChild(option);
				tr = document.getElementById("selectState"+j);
				tr.appendChild(select);
			}
		},
		onFailure: function(){ Alert('Sorry, we have a little problem with the server, please try later');}
  });
	
};

completeState = function(countryId, stateId, num) {
	var req = new Ajax.Request(urlcommon, {
		method: 'get',
		parameters: {method: 'GetStateList', language_id: '1', country_id: countryId},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(checkResp(resp)) {
				
				var mmm = resp.getElementsByTagName('state');
				var name = getNameById(resp.getElementsByTagName('state'), stateId);
				/*var name = resp.getElementsByTagName('state')[stateId-1].childNodes[5].firstChild.nodeValue;*/
				var c = document.getElementById('selectState'+num);
				c.innerHTML = name;			
				
			}			
		},
		onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
	});
};

completeCountry = function(countryId, stateId, num) {
	var req = new Ajax.Request(urlcommon, {
		method: 'get',
		parameters: {method: 'GetCountryList', language_id: '1'},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(checkResp(resp)) {
				
				var name = getNameById(resp.getElementsByTagName('country'), countryId);
				var c = document.getElementById('country'+num);
				c.innerHTML = name;

				completeState(countryId, stateId, num);
				
			}			
		},
		onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
	});
}

getNameById = function (nodeList, id){
	
	for(var i=0; i<nodeList.length; i++) {
		if(nodeList[i].getAttribute('id') == id)
			return nodeList[i].getElementsByTagName('name')[0].firstChild.nodeValue;
	}
};